/**
 * 
 */
package com.itinsys.enma.manager.impl;

import net.sf.ehcache.ObjectExistsException;

import org.springframework.beans.factory.annotation.Autowired;

import com.itinsys.enma.dao.RoleDAO;
import com.itinsys.enma.dao.UserDAO;
import com.itinsys.enma.domain.Role;
import com.itinsys.enma.domain.User;
import com.itinsys.enma.domain.response.PeriodResponse;
import com.itinsys.enma.domain.response.UserResponse;
import com.itinsys.enma.manager.LoginManager;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.UserManager;

/**
 * @author pferraz
 *
 */
public class UserManagerImpl implements UserManager {

	
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private LoginManager loginManager;
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private ResponseManager responseManager;
	
	@Override
	public User getByUserName(String userName, boolean active) {
		return userDAO.getByUserName(userName, active);
	}

	@Override
	public UserResponse assignRolToUser(String userName, String strRole)
	{
		UserResponse resp = new UserResponse();
		if(checkDuplicates(resp, userName)){
			User user=loginManager.existsUserOnLDAP(userName);
			if (user!=null) {
				Role role = this.roleDAO.getById(Integer.parseInt(strRole));
				
				try{
				    user=userDAO.getByUserName(userName,false);
				    user.setRole(role);
			    	user.setActive(true);
			    	userDAO.updateUser(user);
				}catch(ObjectExistsException e){
					user.setUsername(userName);
					user.setActive(true);
					user.setRole(role);
			    	userDAO.add(user);
				}
			    
			  }
			resp=processUserReturn(resp, user);
		}
	
		return resp;
	}

	
	private boolean checkDuplicates(UserResponse userResp, String userName) {
		try{
			getByUserName(userName,true);
			this.responseManager.prepareResponse(userResp,UserResponse.USER_EXISTS);
			return false;
		}catch(ObjectExistsException e){
		}		
		return true;
	}

	/**
	 * @param resp
	 * @param user
	 * @return
	 */
	private UserResponse processUserReturn(UserResponse resp, User user) {
		resp=getAll();
		if (user == null){
			this.responseManager.prepareResponse(resp, UserResponse.USER_NOT_EXISTS);
		}		
		return resp;
	}

	
	@Override
	public UserResponse getAll() {
		UserResponse resp = new UserResponse();
		this.responseManager.prepareResponse(resp, PeriodResponse.OK);
		resp.setUsers(userDAO.listUsers());
		return resp;
	}

	@Override
	public UserResponse updateRoleToUser(String userName, int roleId) {
		
		UserResponse resp = new UserResponse();
		if(!checkDuplicates(resp, userName)){
			Role role = roleDAO.getById(roleId);
			User user = userDAO.getByUserName(userName, true);
			user.setRole(role);	
			userDAO.updateUser(user);
			resp=processUserReturn(resp, user);		
		}
		return resp;
		
	}

	@Override
	public UserResponse delete(String userName) {
		UserResponse resp = new UserResponse();
		User user = userDAO.getByUserName(userName, true);
		user.setActive(false);
		if(userDAO.updateUser(user)){
			resp=getAll();
		}else{
			this.responseManager.prepareResponse(resp, PeriodResponse.ERROR);
		}

		return resp;
	}
}
